| Conditions | 1 |
| Paths | 96 |
| Total Lines | 155 |
| Code Lines | 121 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 6 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | var TableInitSetColumnsHoverEffect = function(tContainer, tableId){ |
||
| 205 | function initInstance(){ |
||
| 206 | // Singleton |
||
| 207 | // Private methods and variables |
||
| 208 | var tail = []; |
||
| 209 | function LoadData(tableContainer, rq){ |
||
| 210 | SetVisability(tableContainer, false); |
||
| 211 | if(window.XMLHttpRequest){ |
||
| 212 | var xmlhttp = new XMLHttpRequest();/* code for IE7+, Firefox, Chrome, Opera, Safari */ |
||
| 213 | }else{ |
||
| 214 | xmlhttp = new window.ActiveXObject("Microsoft.XMLHTTP");/*code for IE6, IE5 */ |
||
| 215 | } |
||
| 216 | for(var i = 0; i < tail.length; i++){ |
||
| 217 | var ex_xmlhttp = tail.shift(); |
||
| 218 | ex_xmlhttp.abort(); |
||
| 219 | } |
||
| 220 | xmlhttp.onreadystatechange = function(){ |
||
| 221 | if(xmlhttp.readyState === 4 && xmlhttp.status === 200){ |
||
| 222 | var d = JSON.parse(xmlhttp.responseText); |
||
| 223 | instance.rq = rq; |
||
| 224 | instance.Draw(tableContainer, d); |
||
| 225 | } |
||
| 226 | }; |
||
| 227 | xmlhttp.open("GET", RequestToUrl(rq), true); |
||
| 228 | xmlhttp.send(); |
||
| 229 | tail.push(xmlhttp); //put at tail to can abort later any previous |
||
| 230 | } |
||
| 231 | function Init(tableId){ |
||
| 232 | var tContainer = document.getElementById(tableId); |
||
| 233 | if(iePrior(9)){ |
||
| 234 | TableInitSetColumnsHoverEffect(tContainer, tableId); |
||
| 235 | } |
||
| 236 | var tfoot = tContainer.getElementsByTagName("tfoot")[0]; |
||
| 237 | TableHelperProcessPaginationLinks(tfoot); |
||
| 238 | } |
||
| 239 | function Draw(tableContainer, d){ |
||
| 240 | DrawSection(tableContainer, d.body); |
||
| 241 | DrawSection(tableContainer, d.footer, "tfoot"); |
||
| 242 | instance.LoadEndCalback(tableContainer); |
||
| 243 | SetVisability(tableContainer, true); |
||
| 244 | if(this.rq !== null){ |
||
| 245 | var hover = document.getElementById(this.rq.tableId) |
||
| 246 | .getElementsByTagName("th")[this.rq.colNo].lang; |
||
| 247 | if(hover){ |
||
| 248 | instance.ColumnHover(tableContainer, this.rq.colNo); |
||
| 249 | } |
||
| 250 | } |
||
| 251 | } |
||
| 252 | function DrawSection(tableContainer, dt, tSection){ |
||
| 253 | var section = tSection === "tfoot" ? "tfoot" : "tbody"; |
||
| 254 | var tSec = document.getElementById(tableContainer) |
||
| 255 | .getElementsByTagName(section)[0]; |
||
| 256 | TableHelperDrawSectionClear(tSec, iePrior(9)); |
||
| 257 | for(var i = 0; i < dt.length; i++){ |
||
| 258 | var row = dt[i]; |
||
| 259 | var tRow = document.createElement("tr"); |
||
| 260 | TableHelperDrawSectionRow(row, tRow); |
||
| 261 | tSec.appendChild(tRow); |
||
| 262 | if(section === "tfoot"){ |
||
| 263 | TableHelperProcessPaginationLinks(tSec); |
||
| 264 | } |
||
| 265 | instance.AppendRowCalback(tableContainer); |
||
| 266 | } |
||
| 267 | } |
||
| 268 | |||
| 269 | var BuildRequest = TableHelperBuildRequest; |
||
| 270 | var FilterGetTableId = TableHelperFilterGetTableId; |
||
| 271 | var getParent = TableHelperGetParent; |
||
| 272 | var iePrior = TableHelperIfPrior; |
||
| 273 | var RequestToUrl = TableHelperRequestToUrl; |
||
| 274 | var SetVisability = TableHelperSetVisability; |
||
| 275 | |||
| 276 | return { |
||
| 277 | rq: null, |
||
| 278 | strAsc: String.fromCharCode(9650), //▲ |
||
| 279 | strDesc: String.fromCharCode(9660),//▼ |
||
| 280 | ReloadData: function(tableId){ |
||
| 281 | var request = {}; |
||
| 282 | BuildRequest(request, tableId, this.strDesc); |
||
| 283 | LoadData(tableId, request); |
||
| 284 | }, |
||
| 285 | Filter: function(field){ |
||
| 286 | var crntTableId = FilterGetTableId(field); |
||
| 287 | if(crntTableId !== null){ |
||
| 288 | var request = {}; |
||
| 289 | var exRq = this.rq; |
||
| 290 | BuildRequest(request, crntTableId, this.strDesc); |
||
| 291 | if(exRq === null || |
||
| 292 | request.filter !== exRq.filter || |
||
| 293 | request.filterBy !== exRq.filterBy |
||
| 294 | ){ |
||
| 295 | LoadData(crntTableId, request); |
||
| 296 | } |
||
| 297 | } |
||
| 298 | }, |
||
| 299 | GoPage: function(lnk){ |
||
| 300 | var request = {}; |
||
| 301 | var table = getParent(lnk, "table"); |
||
| 302 | var crntTableId = table.getAttribute("id"); |
||
| 303 | BuildRequest(request, crntTableId, this.strDesc); |
||
| 304 | //check & serve pagination jump links |
||
| 305 | var jumpDir = lnk.innerHTML.trim().substr(0, 1); |
||
| 306 | if(jumpDir === "+" || jumpDir === "-"){ |
||
| 307 | var current = table.querySelector("tfoot .paging .a").innerHTML; |
||
| 308 | var jump = lnk.innerHTML.replace("K", "000").replace("M", "000000000"); |
||
| 309 | var jumpPage = (parseInt(current) + parseInt(jump)); |
||
| 310 | lnk.parentNode.setAttribute("data-page", jumpPage); |
||
| 311 | lnk.style.transform = "none"; |
||
| 312 | } |
||
| 313 | request.pageNo = lnk.parentNode.hasAttribute("data-page") ? |
||
| 314 | lnk.parentNode.getAttribute("data-page") : |
||
| 315 | lnk.innerHTML; |
||
| 316 | LoadData(crntTableId, request, this.strDesc); |
||
| 317 | return false; |
||
| 318 | }, |
||
| 319 | Export: function(lnk, eType){ |
||
| 320 | var request = {}; |
||
| 321 | var crntTableId = getParent(lnk, "table").getAttribute("id"); |
||
| 322 | BuildRequest(request, crntTableId, this.strDesc); |
||
| 323 | request.exportType = ["CSV", "Excel"].indexOf(eType) >= 0 ? |
||
| 324 | eType : |
||
| 325 | "csv"; |
||
| 326 | window.open(RequestToUrl(request)); |
||
| 327 | return false; |
||
| 328 | }, |
||
| 329 | Sort: function(colNo, lnk){ |
||
| 330 | var request = {}; |
||
| 331 | var crntTableId = getParent(lnk, "table").getAttribute("id"); |
||
| 332 | BuildRequest(request, crntTableId, this.strDesc); |
||
| 333 | if(Math.round(colNo) === request.colNo){ |
||
| 334 | request.colOrd = (request.colOrd === "asc" ? "desc" : "asc"); |
||
| 335 | }else{ |
||
| 336 | request.colNo = Math.round(colNo); |
||
| 337 | request.colOrd = "asc"; |
||
| 338 | } |
||
| 339 | LoadData(crntTableId, request); |
||
| 340 | /* Clear and add new sort arrow */ |
||
| 341 | var headSpans = getParent(lnk, "thead").getElementsByTagName("span"); |
||
| 342 | var length = headSpans.length; |
||
| 343 | for(var i = 0; i < length; i++){ |
||
| 344 | headSpans[i].innerHTML = ""; |
||
| 345 | } |
||
| 346 | lnk.getElementsByTagName("span")[0].innerHTML = (request.colOrd === "desc" ? this.strDesc : this.strAsc); |
||
| 347 | }, |
||
| 348 | |||
| 349 | ColumnHover: function(tableContainer, index){ |
||
| 350 | if(!iePrior(9)){ |
||
| 351 | TableHelperColumnHover.call(this, tableContainer, index); |
||
| 352 | } |
||
| 353 | }, |
||
| 354 | Draw: Draw, |
||
| 355 | LoadEndCalback: function(){},/*Allows override: function(tableId){if(tableId){...}}*/ |
||
| 356 | AppendRowCalback: function(){},/*Allows override: function(tableId){if(tableId){...}}*/ |
||
| 357 | init: Init |
||
| 358 | }; |
||
| 359 | } |
||
| 360 | return { |
||
| 371 |
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: